package com.yz.lzq.liwushuo.ui.setting;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.utils.ActivityUtil;
import com.yz.lzq.liwushuo.ui.main.MainActivity;

import java.util.ArrayList;
import java.util.List;

public class SettingActivity extends AppCompatActivity implements View.OnClickListener, SettingContract.SettingActivityViewInterface {
    //上下文
    private Context mContext = this;
    private LayoutInflater inflater = null;
    private ImageView imageView_dialog_sex_close;
    private ImageView imageView_dialog_sex_boy;
    private ImageView imageView_dialog_sex_girl;
    private TextView textView_dialog_sex_boy;
    private TextView textView_dialog_sex_girl;
    private TextView textviwe_sexdialog_next;
    private CheckBox checkbox_dialog_sex_boy;
    private CheckBox checkbox_dialog_sex_girl;
    private SettingActivityPresenter presenter = null;
    private FrameLayout frameLayout_settings;
    private int screenHeight;
    private int screenWidth;
    private PopupWindow popupWindowIdentit;
    private PopupWindow popupWindowSex;
    private ImageView imageView_dialog_identity_back;

    private int genneder;
    private int generation;
    private TextView textView_dialog_identity_chuzhongsheng;
    private CheckBox checkbox_dialog_identity_chuzhongsheng;
    private TextView textView_dialog_identity_gaozhongsheng;
    private CheckBox checkbox_dialog_identity_gaozhongsheng;
    private TextView textView_dialog_identity_daxuesheng;
    private CheckBox checkbox_dialog_identity_daxuesheng;
    private TextView textView_dialog_identity_zhichangxinren;
    private CheckBox checkbox_dialog_identity_zhichangxinren;
    private TextView textView_dialog_identity_zishenggongzuo;
    private CheckBox checkbox_dialog_identity_zishenggongzuo;
    private TextView textView_dialog_identity_saveData;
    //用来存储选择身份的选项名
    private List<TextView> mList_TextView = new ArrayList<>();
    //用来存储选择身份的checkbox
    private List<CheckBox> mList_CheckBox = new ArrayList<>();
    private ImageView imageView_dialog_identity_close;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        //获取屏幕的高度
        screenHeight = getResources().getDisplayMetrics().heightPixels;
        //获取屏幕的宽度
        screenWidth = getResources().getDisplayMetrics().widthPixels;
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        frameLayout_settings = (FrameLayout) findViewById(R.id.frameLayout_Settings);
        presenter = new SettingActivityPresenter(mContext, this);
    }

    /**
     * activity的窗口获取焦点的时候
     *
     * @param hasFocus
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        // 弹出PopupWindow的具体代码
        showSexPoPupWindow(frameLayout_settings);

    }

    /**
     * 显示 选择身份的popupWindow
     *
     * @param parent 父布局
     */
    public void showIdentityPoPupWindow(View parent) {
        View view = inflater.inflate(R.layout.dialog_person, null);
        imageView_dialog_identity_back = (ImageView) view.findViewById(R.id.imageView_dialog_identity_back);
        imageView_dialog_identity_close = (ImageView) view.findViewById(R.id.imageView_dialog_identity_close);
        textView_dialog_identity_chuzhongsheng = (TextView) view.findViewById(R.id.textView_dialog_identity_chuzhongsheng);
        checkbox_dialog_identity_chuzhongsheng = (CheckBox) view.findViewById(R.id.checkbox_dialog_identity_chuzhongsheng);
        textView_dialog_identity_gaozhongsheng = (TextView) view.findViewById(R.id.textView_dialog_identity_gaozhongsheng);
        checkbox_dialog_identity_gaozhongsheng = (CheckBox) view.findViewById(R.id.checkbox_dialog_identity_gaozhongsheng);
        textView_dialog_identity_daxuesheng = (TextView) view.findViewById(R.id.textView_dialog_identity_daxuesheng);
        checkbox_dialog_identity_daxuesheng = (CheckBox) view.findViewById(R.id.checkbox_dialog_identity_daxuesheng);
        textView_dialog_identity_zhichangxinren = (TextView) view.findViewById(R.id.textView_dialog_identity_zhichangxinren);
        checkbox_dialog_identity_zhichangxinren = (CheckBox) view.findViewById(R.id.checkbox_dialog_identity_zhichangxinren);
        textView_dialog_identity_zishenggongzuo = (TextView) view.findViewById(R.id.textView_dialog_identity_zishenggongzuo);
        checkbox_dialog_identity_zishenggongzuo = (CheckBox) view.findViewById(R.id.checkbox_dialog_identity_zishenggongzuo);
        textView_dialog_identity_saveData = (TextView) view.findViewById(R.id.textView_dialog_identity_saveData);

        imageView_dialog_identity_close.setOnClickListener(this);
        imageView_dialog_identity_back.setOnClickListener(this);
        textView_dialog_identity_saveData.setOnClickListener(this);

        textView_dialog_identity_gaozhongsheng.setOnClickListener(this);
        textView_dialog_identity_zishenggongzuo.setOnClickListener(this);
        textView_dialog_identity_daxuesheng.setOnClickListener(this);
        textView_dialog_identity_chuzhongsheng.setOnClickListener(this);
        textView_dialog_identity_zhichangxinren.setOnClickListener(this);
        mList_TextView.add(textView_dialog_identity_chuzhongsheng);
        mList_TextView.add(textView_dialog_identity_gaozhongsheng);
        mList_TextView.add(textView_dialog_identity_daxuesheng);
        mList_TextView.add(textView_dialog_identity_zhichangxinren);
        mList_TextView.add(textView_dialog_identity_zishenggongzuo);
        checkbox_dialog_identity_zishenggongzuo.setOnClickListener(this);
        checkbox_dialog_identity_zhichangxinren.setOnClickListener(this);
        checkbox_dialog_identity_daxuesheng.setOnClickListener(this);
        checkbox_dialog_identity_gaozhongsheng.setOnClickListener(this);
        checkbox_dialog_identity_chuzhongsheng.setOnClickListener(this);

        mList_CheckBox.add(checkbox_dialog_identity_chuzhongsheng);
        mList_CheckBox.add(checkbox_dialog_identity_gaozhongsheng);
        mList_CheckBox.add(checkbox_dialog_identity_daxuesheng);
        mList_CheckBox.add(checkbox_dialog_identity_zhichangxinren);
        mList_CheckBox.add(checkbox_dialog_identity_zishenggongzuo);
        presenter.showDefaultIdentityData();
        popupWindowIdentit = new PopupWindow(view, (int) (screenWidth * 0.8), (int) (screenHeight * 0.7));
        nomalPopupWindowSetting(parent, view, popupWindowIdentit);
    }

    /**
     * 显示 选择性别的popupWindow
     *
     * @param parent 父布局
     */
    public void showSexPoPupWindow(View parent) {
        //这里也可以创建一个popupWindow
        //第一步  创建（其中的一种方式）
      /*  View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);
        PopupWindwo popWnd = PopupWindow(context);
        popWnd.setContentView(contentView);
        popWnd.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        //第二步   设置显示位置 （调用显示函数）
            //相对某个控件的位置（正左下方），无偏移
    showAsDropDown(View anchor)：
    //相对某个控件的位置，有偏移;xoff表示x轴的偏移，正值表示向左，负值表示向右；yoff表示相对y轴的偏移，正值是向下，负值是向上；
    showAsDropDown(View anchor, int xoff, int yoff)：
    //相对于父控件的位置（例如正中央Gravity.CENTER，下方Gravity.BOTTOM等），可以设置偏移或无偏移
    showAtLocation(View parent, int gravity, int x, int y)：
        */
//第三步  调用其他的函数
      /*  public void dismiss()
        //另外几个函数，这里不讲其意义，下篇细讲
        public void setFocusable(boolean focusable)
        public void setTouchable(boolean touchable)
        public void setOutsideTouchable(boolean touchable)
        public void setBackgroundDrawable(Drawable background)*/
        View view = inflater.inflate(R.layout.dialog_sex, null);
        imageView_dialog_sex_close = (ImageView) view.findViewById(R.id.imageView_dialog_sex_close);
        imageView_dialog_sex_boy = (ImageView) view.findViewById(R.id.imageView_dialog_sex_boy);
        imageView_dialog_sex_girl = (ImageView) view.findViewById(R.id.imageView_dialog_sex_girl);
        textView_dialog_sex_boy = (TextView) view.findViewById(R.id.textView_dialog_sex_boy);
        textView_dialog_sex_girl = (TextView) view.findViewById(R.id.textView_dialog_sex_girl);
        textviwe_sexdialog_next = (TextView) view.findViewById(R.id.textviwe_sexdialog_next);
        checkbox_dialog_sex_boy = (CheckBox) view.findViewById(R.id.checkbox_dialog_sex_boy);
        checkbox_dialog_sex_girl = (CheckBox) view.findViewById(R.id.checkbox_dialog_sex_girl);
        imageView_dialog_sex_close.setOnClickListener(this);
        textviwe_sexdialog_next.setOnClickListener(this);
        checkbox_dialog_sex_girl.setOnClickListener(this);
        checkbox_dialog_sex_boy.setOnClickListener(this);
        textView_dialog_sex_boy.setOnClickListener(this);
        textView_dialog_sex_girl.setOnClickListener(this);
        imageView_dialog_sex_boy.setOnClickListener(this);
        imageView_dialog_sex_girl.setOnClickListener(this);
        presenter.showDefaultSexData();
        // 下面是两种方法得到宽度和高度
        // getWindow().getDecorView().getWidth()
        //mContext.getResources().getDisplayMetrics().heightPixels

        //创建popupwindow 高和宽都为屏幕的0.8
        popupWindowSex = new PopupWindow(view, (int) (screenWidth * 0.8), (int) (screenHeight * 0.7));
        //调用popupwindow  的常规设置方法
        nomalPopupWindowSetting(parent, view, popupWindowSex);


    }

    /**
     * popupwindow  的常规设置
     *
     * @param parent
     * @param view
     * @param popupWindow
     */
    private void nomalPopupWindowSetting(View parent, View view, PopupWindow popupWindow) {
        view.setFocusable(true);
        //设置可操作
        popupWindow.setTouchable(true);
        //设置点击外面空白区域不隐藏界面
        popupWindow.setOutsideTouchable(false);
        //设置背景
        popupWindow.setBackgroundDrawable(new ColorDrawable(0X00000000));
        popupWindow.setAnimationStyle(R.style.animate_dialog);


        //相对于父控件的位置（例如正中央Gravity.CENTER，下方Gravity.BOTTOM等），可以设置偏移或无偏移
        popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textviwe_sexdialog_next:
                popupWindowSex.dismiss();
                showIdentityPoPupWindow(view);
                break;
            case R.id.imageView_dialog_identity_back:
                popupWindowIdentit.dismiss();
                showSexPoPupWindow(view);
                break;
            case R.id.checkbox_dialog_sex_boy:
             /*   checkbox_dialog_sex_boy.setChecked(true);
                checkbox_dialog_sex_girl.setChecked(false);
                break;*/
            case R.id.imageView_dialog_sex_boy:
             /*   checkbox_dialog_sex_boy.setChecked(true);
                checkbox_dialog_sex_girl.setChecked(false);
                break;*/
            case R.id.textView_dialog_sex_boy:
                checkbox_dialog_sex_boy.setChecked(true);
                checkbox_dialog_sex_girl.setChecked(false);
                break;
            case R.id.imageView_dialog_sex_girl:
            /*    checkbox_dialog_sex_girl.setChecked(true);
                checkbox_dialog_sex_boy.setChecked(false);

                break;*/
            case R.id.textView_dialog_sex_girl:
           /*     checkbox_dialog_sex_girl.setChecked(true);
                checkbox_dialog_sex_boy.setChecked(false);

                break;*/
            case R.id.checkbox_dialog_sex_girl:
                checkbox_dialog_sex_girl.setChecked(true);
                checkbox_dialog_sex_boy.setChecked(false);
                break;
            case R.id.textView_dialog_identity_chuzhongsheng:
                for (int i = 0; i < mList_TextView.size(); i++) {
                    if (TextUtils.equals(textView_dialog_identity_chuzhongsheng.getText(),
                            mList_TextView.get(i).getText())) {
                        mList_TextView.get(i).setTextSize(17);
                        mList_TextView.get(i).setTextColor(getResources().getColor(R.color.red));

                        mList_CheckBox.get(i).setChecked(true);
                    } else {
                        mList_TextView.get(i).setTextSize(14);
                        mList_TextView.get(i).setTextColor(getResources().getColor(android.R.color.secondary_text_dark));

                        mList_CheckBox.get(i).setChecked(false);
                    }
                }
                break;
            case R.id.textView_dialog_identity_gaozhongsheng:
                for (int i = 0; i < mList_TextView.size(); i++) {
                    if (TextUtils.equals(textView_dialog_identity_gaozhongsheng.getText(),
                            mList_TextView.get(i).getText())) {
                        mList_TextView.get(i).setTextSize(17);
                        mList_TextView.get(i).setTextColor(getResources().getColor(R.color.red));

                        mList_CheckBox.get(i).setChecked(true);
                    } else {
                        mList_TextView.get(i).setTextSize(14);
                        mList_TextView.get(i).setTextColor(getResources().getColor(android.R.color.secondary_text_dark));

                        mList_CheckBox.get(i).setChecked(false);
                    }
                }
                break;
            case R.id.textView_dialog_identity_daxuesheng:
                for (int i = 0; i < mList_TextView.size(); i++) {
                    if (TextUtils.equals(textView_dialog_identity_daxuesheng.getText(),
                            mList_TextView.get(i).getText())) {
                        mList_TextView.get(i).setTextSize(17);
                        mList_TextView.get(i).setTextColor(getResources().getColor(R.color.red));

                        mList_CheckBox.get(i).setChecked(true);
                    } else {
                        mList_TextView.get(i).setTextSize(14);
                        mList_TextView.get(i).setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
                        mList_CheckBox.get(i).setChecked(false);
                    }
                }
                break;
            case R.id.textView_dialog_identity_zhichangxinren:
                for (int i = 0; i < mList_TextView.size(); i++) {
                    if (TextUtils.equals(textView_dialog_identity_zhichangxinren.getText(),
                            mList_TextView.get(i).getText())) {
                        mList_TextView.get(i).setTextSize(17);
                        mList_TextView.get(i).setTextColor(getResources().getColor(R.color.red));

                        mList_CheckBox.get(i).setChecked(true);
                    } else {
                        mList_TextView.get(i).setTextSize(14);
                        mList_TextView.get(i).setTextColor(getResources().getColor(android.R.color.secondary_text_dark));

                        mList_CheckBox.get(i).setChecked(false);
                    }
                }
                break;
            case R.id.textView_dialog_identity_zishenggongzuo:
                for (int i = 0; i < mList_TextView.size(); i++) {
                    if (TextUtils.equals(textView_dialog_identity_zishenggongzuo.getText(),
                            mList_TextView.get(i).getText())) {
                        mList_TextView.get(i).setTextSize(17);
                        mList_TextView.get(i).setTextColor(getResources().getColor(R.color.red));
                        mList_CheckBox.get(i).setChecked(true);
                    } else {
                        mList_TextView.get(i).setTextSize(14);
                        mList_TextView.get(i).setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
                        mList_CheckBox.get(i).setChecked(false);
                    }
                }
                break;
            case R.id.checkbox_dialog_identity_chuzhongsheng:
                for (int i = 0; i < mList_CheckBox.size(); i++) {
                    if (checkbox_dialog_identity_chuzhongsheng.getId() == mList_CheckBox.get(i).getId()) {
                        mList_TextView.get(i).setTextSize(17);
                        mList_TextView.get(i).setTextColor(getResources().getColor(R.color.red));
                        mList_CheckBox.get(i).setChecked(true);
                    } else {
                        mList_TextView.get(i).setTextSize(14);
                        mList_TextView.get(i).setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
                        mList_CheckBox.get(i).setChecked(false);
                    }
                }
                break;
            case R.id.checkbox_dialog_identity_gaozhongsheng:
                for (int i = 0; i < mList_CheckBox.size(); i++) {
                    if (checkbox_dialog_identity_gaozhongsheng.getId() == mList_CheckBox.get(i).getId()) {
                        mList_TextView.get(i).setTextSize(17);
                        mList_TextView.get(i).setTextColor(getResources().getColor(R.color.red));
                        mList_CheckBox.get(i).setChecked(true);
                    } else {
                        mList_TextView.get(i).setTextSize(14);
                        mList_TextView.get(i).setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
                        mList_CheckBox.get(i).setChecked(false);
                    }
                }
                break;
            case R.id.checkbox_dialog_identity_daxuesheng:
                for (int i = 0; i < mList_CheckBox.size(); i++) {
                    if (checkbox_dialog_identity_daxuesheng.getId() == mList_CheckBox.get(i).getId()) {
                        mList_TextView.get(i).setTextSize(17);
                        mList_TextView.get(i).setTextColor(getResources().getColor(R.color.red));
                        mList_CheckBox.get(i).setChecked(true);
                    } else {
                        mList_TextView.get(i).setTextSize(14);
                        mList_TextView.get(i).setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
                        mList_CheckBox.get(i).setChecked(false);
                    }
                }
                break;
            case R.id.checkbox_dialog_identity_zhichangxinren:
                for (int i = 0; i < mList_CheckBox.size(); i++) {
                    if (checkbox_dialog_identity_zhichangxinren.getId() == mList_CheckBox.get(i).getId()) {
                        mList_TextView.get(i).setTextSize(17);
                        mList_TextView.get(i).setTextColor(getResources().getColor(R.color.red));
                        mList_CheckBox.get(i).setChecked(true);
                    } else {
                        mList_TextView.get(i).setTextSize(14);
                        mList_TextView.get(i).setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
                        mList_CheckBox.get(i).setChecked(false);
                    }
                }
                break;
            case R.id.checkbox_dialog_identity_zishenggongzuo:
                for (int i = 0; i < mList_CheckBox.size(); i++) {
                    if (checkbox_dialog_identity_zishenggongzuo.getId() == mList_CheckBox.get(i).getId()) {
                        mList_TextView.get(i).setTextSize(17);
                        mList_TextView.get(i).setTextColor(getResources().getColor(R.color.red));
                        mList_CheckBox.get(i).setChecked(true);
                    } else {
                        mList_TextView.get(i).setTextSize(14);
                        mList_TextView.get(i).setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
                        mList_CheckBox.get(i).setChecked(false);
                    }
                }
                break;


            case R.id.textView_dialog_identity_saveData:

                presenter.writeUserSharedPerfence();
                popupWindowIdentit.dismiss();
                ActivityUtil.statActivity(this, MainActivity.class);
                finish();
//                Toast.makeText(mContext, "现在拿到的数据是：>>>>>>>>>>>>>>>>>>>>>genneder" + genneder + "=======generation" + generation, Toast.LENGTH_SHORT).show();
                break;
            case R.id.imageView_dialog_sex_close:
            case R.id.imageView_dialog_identity_close:
//                presenter.writeUserSharedPerfence();
                popupWindowIdentit.dismiss();
                ActivityUtil.statActivity(this, MainActivity.class);
                finish();
                break;
        }


    }

    /**
     * @return 返回用户设置的性别
     */
    @Override
    public int getGender() {
        if (checkbox_dialog_sex_boy.isChecked() && !checkbox_dialog_sex_girl.isChecked()) {
            return 1;
        } else if (!checkbox_dialog_sex_boy.isChecked() && checkbox_dialog_sex_girl.isChecked()) {
            return 2;
        }
        return 0;
    }

    /**
     * @return 返回用户设置的身份
     */
    @Override
    public int getGeneration() {
        for (int i = 0; i < mList_CheckBox.size(); i++) {
            if (mList_CheckBox.get(i).isChecked()) {
                return i;
            }
        }
        return 99;
    }

    /**
     * @param gender 设置默认的用户的性别
     */
    @Override
    public void setSexDefaultData(int gender) {
        if (99 != gender) {
            if (gender == 1) {
                checkbox_dialog_sex_boy.setChecked(true);
                checkbox_dialog_sex_girl.setChecked(false);
            } else {
                checkbox_dialog_sex_boy.setChecked(false);
                checkbox_dialog_sex_girl.setChecked(true);
            }
        } else {
            checkbox_dialog_sex_boy.setChecked(true);
            checkbox_dialog_sex_girl.setChecked(false);

        }

    }

    /**
     * @param generation 设置默认的用户的身份
     */
    @Override
    public void setIdentityDefaultData(int generation) {
        if (99 != generation) {

            mList_CheckBox.get(generation).setChecked(true);
            mList_TextView.get(generation).setTextSize(17);
            mList_TextView.get(generation).setTextColor(getResources().getColor(R.color.red));

        } else {
            checkbox_dialog_identity_daxuesheng.setChecked(true);
            textView_dialog_identity_daxuesheng.setTextSize(17);
            textView_dialog_identity_daxuesheng.setTextColor(getResources().getColor(R.color.red));

        }
    }
}
